<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- https://juejin.cn/post/6937469222251560990#heading-11 -->
    <script>
        function curry(fn, len = fn.length) {
            return _curry(fn, len)
        }

        function _curry(fn, len, ...arg) {
            return function (...params) {
                let _arg = [...arg, ...params];//arg：上次参数，params：本次参数
                // console.log('_arg len', _arg.length);
                // console.log('arg len', arg.length);
                // console.log('params len', params.length);
                console.log('_arg',_arg);
                console.log('arg',arg);
                console.log('params',params);
                if (_arg.length >= len) {
                    console.log('execute');
                    return fn.apply(this, _arg)
                } else {
                    console.error('wait');

                    return _curry.call(this, fn, len, ..._arg)
                }
            }
        }

        let fn = curry(function (a, b, c, d, e) {
            console.log(a + b + c + d + e)
        })

        // fn(1, 2, 3, 4, 5)  // 15
        // fn(1, 2)(3, 4, 5)
        // fn(1, 2)(3)(4)(5)
        // fn(1)(2)(3)(4)(5)
        fn(1, 2)(3,4,5)

    </script>
</body>

</html>